package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.product.service.ManagerService;
import com.atguigu.gmall.product.service.TrademarkService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ：MJS
 * @date ：Created in 2021/5/8  20:32
 * @description：TODO
 * 后台管理中心
 */
//@CrossOrigin//跨域问题
@RestController
@RequestMapping("/admin/product")
public class ManagerController {

    @Autowired
    private ManagerService managerService;
    @Autowired
    private TrademarkService trademarkService;
    //查询一级分类的集合
    @GetMapping("/getCategory1")
    public Result getCategory1(){
        List<BaseCategory1>baseCategory1List=managerService.getCategory1();
        return Result.ok(baseCategory1List);
    }
    //查询二级分类的集合
    @GetMapping("/getCategory2/{category1Id}")
    public Result getCategory2(@PathVariable Long category1Id){
        List<BaseCategory2>baseCategory2List=managerService.getCategory2(category1Id);
        return Result.ok(baseCategory2List);
    }
    //查询三级分类的集合
    @GetMapping("/getCategory3/{category2Id}")
    public Result getCategory3(@PathVariable Long category2Id){
        List<BaseCategory3>baseCategory3List=managerService.getCategory3(category2Id);
        return Result.ok(baseCategory3List);
    }
    //根据一二三级分类Id  层级查询  平台属性及属性值表
    @GetMapping("/attrInfoList/{category1Id}/{category2Id}/{category3Id}")
    public Result attrInfoList(@PathVariable Long category1Id,
                               @PathVariable Long category2Id,
                               @PathVariable Long category3Id){
        List<BaseAttrInfo>BaseAttrInfoList=managerService.attrInfoList(category1Id,category2Id,category3Id);
        return Result.ok(BaseAttrInfoList);
    }
    //添加平台属性及值
    @PostMapping("/saveAttrInfo")
    public Result saveAttrInfo(@RequestBody BaseAttrInfo baseAttrInfo){
       managerService.saveAttrInfo(baseAttrInfo);
       return Result.ok();
    }
    //根据属性id查询平台属性
    @GetMapping("/getAttrValueList/{attrId}")
    public Result getAttrValueList(@PathVariable Long attrId){
        List<BaseAttrValue>baseAttrValueList=managerService.getAttrValueList(attrId);
        return Result.ok(baseAttrValueList);
    }
    //分页查询品牌列表
    @GetMapping("/baseTrademark/{page}/{limit}")
    public Result baseTrademark(@PathVariable Integer page,
                                @PathVariable Integer limit){
        //分页对象
        IPage iPage=managerService.baseTrademark(page,limit);
        return Result.ok(iPage);
    }
    //添加品牌
    @PostMapping("/baseTrademark/save")
    public Result save(@RequestBody BaseTrademark baseTrademark){
        trademarkService.saveTrademark(baseTrademark);
        return Result.ok();
    }
    //根据id查询品牌
    @GetMapping("/baseTrademark/get/{id}")
    public Result getById(@PathVariable String id){
        BaseTrademark baseTrademark=trademarkService.getTrademarkById(id);
        return Result.ok(baseTrademark);
    }
    //修改品牌
    @PutMapping("/baseTrademark/update")
    public Result updateById(@RequestBody BaseTrademark baseTrademark){
        trademarkService.updateById(baseTrademark);
        return Result.ok();
    }
    //根据id删除品牌
    @DeleteMapping("/baseTrademark/remove/{id}")
    public Result remove(@PathVariable Long id){
        trademarkService.remove(id);
        return Result.ok();
    }
    //获取spu分页列表
    @GetMapping("/{page}/{limit}")
    public Result getSpuByPage(@PathVariable Integer page,
                               @PathVariable Integer limit,
                               Long category3Id){
        IPage iPage=managerService.getSpuByPage(page,limit,category3Id);
        return Result.ok(iPage);
    }
    //获取销售属性
    @GetMapping("/baseSaleAttrList")
    public Result baseSaleAttrList(){
        List<BaseSaleAttr>baseSaleAttrList=managerService.baseSaleAttrList();
        return Result.ok(baseSaleAttrList);
    }
    //获取品牌属性
    @GetMapping("/baseTrademark/getTrademarkList")
    public Result getTrademarkList(){
        List<BaseTrademark>baseTrademarkList=managerService.getTrademarkList();
        return Result.ok(baseTrademarkList);
    }
    //添加spu
    @PostMapping("/saveSpuInfo")
    public Result saveSpuInfo(@RequestBody SpuInfo spuInfo){
        managerService.saveSpuInfo(spuInfo);
        return Result.ok();
    }
    //根据spuId获取图片列表  显示Sku添加页面   目标为了让管理员选择
    @GetMapping("/spuImageList/{spuId}")
    public Result spuImageList(@PathVariable Long spuId){
        List<SpuImage>spuImageList=managerService.spuImageList(spuId);
        return Result.ok(spuImageList);
    }
    //根据SpuId查询销售属性及属性值
    @GetMapping("/spuSaleAttrList/{spuId}")
    public Result spuSaleAttrList(@PathVariable Long spuId){
        List<SpuSaleAttr>spuSaleAttrList=managerService.spuSaleAttrList(spuId);
        return Result.ok(spuSaleAttrList);
    }
    //添加库存
    @PostMapping("/saveSkuInfo")
    public Result saveSkuInfo(@RequestBody SkuInfo skuInfo){
        managerService.saveSkuInfo(skuInfo);
        return Result.ok();
    }
    //获取sku分页列表
    @GetMapping("/list/{page}/{limit}")
    public Result list(@PathVariable Integer page,
                       @PathVariable Integer limit){
        IPage<SkuInfo> iPage=managerService.list(page,limit);
        return Result.ok(iPage);
    }
    //上架
    @GetMapping("/onSale/{skuId}")
    public Result onSale(@PathVariable Long skuId){
        managerService.onSale(skuId);
        return Result.ok();
    }

    //下架
    @GetMapping("/cancelSale/{skuId}")
    public Result cancelSale(@PathVariable Long skuId){
        managerService.cancelSale(skuId);
        return Result.ok();
    }
}
